#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

#include "pch.h"
#include <versionhelpers.h>
#include <psapi.h>


#define ID_HOTKEY_TEST		1
#define L_WNDCLASS_NAME		_T("CodeBlocksWindowsApp")

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT OnTest(HWND hwnd);


int WINAPI _tWinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgument, int nCmdShow)
{
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wincl;

    TCHAR cwd[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, cwd);
    _putts(cwd);

	if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, cwd)))
	{
		_putts(cwd);
	}

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = L_WNDCLASS_NAME;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx(
               0,
               L_WNDCLASS_NAME,
               _T("Code::Blocks Template Windows App"),
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT,
               CW_USEDEFAULT,
               544,
               375,
               HWND_DESKTOP,
               NULL,
               hThisInstance,
               NULL
           );

    ShowWindow(hwnd, nCmdShow);

    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return static_cast<int>(msg.wParam);
}

void CloseDialog(HWND hwnd)
{
	UnregisterHotKey(hwnd, ID_HOTKEY_TEST);
	PostQuitMessage(0);
}

LRESULT OnKeyDown(UINT vk, UINT repeat, BOOL isExtended)
{
    if (vk == VK_CAPITAL)
    {
        BYTE buf[256] = {0};
        GetKeyboardState(buf);
        printf("GetKeyboardState: CAPS %s\n", (buf[VK_CAPITAL] & 1) ? "on" : "off");

        SHORT n = GetKeyState(VK_CAPITAL);
        printf("GetKeyState     : CAPS %s\n", (n & 1) ? "on" : "off");
    }

    printf("VK: %u (0x%02X), Repeat: %u, Extended Key: %s\n",
        vk, vk, repeat, isExtended ? "yes" : "no");
    if (vk == VK_ESCAPE) CloseDialog(0);
    return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_HOTKEY:
        return OnTest(hwnd);
        break;
    case WM_KEYDOWN:
        return OnKeyDown(wParam, (lParam & 0x7f), lParam & (1 << 24));
        break;
    case WM_DESTROY:
        CloseDialog(hwnd);
        break;
    case WM_CREATE:
        RegisterHotKey(hwnd, ID_HOTKEY_TEST, MOD_CONTROL, VK_F5);
        break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

LRESULT OnTest(HWND hwnd)
{
    BOOL f = 0;
    TCHAR sz[200];

    IsWow64Process(GetCurrentProcess(), &f);
    _stprintf(sz, "IsWow64Process=%d\n", f);
    MessageBox(hwnd, sz, NULL, 0);

    return 0;
}
